/* eslint-disable no-extra-parens */
import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}, tts = false) => {
	if (!form || !workstep || !workstep.do) {
		return;
	}
	form.aiSpeak = workstep.do?.data; // 1
	form.aiSpeakVideo = workstep.do?.video;

	form.waitTime = workstep.timeout; // 2

	form.collectShortVideoTime = workstep.after?.data?.data?.data?.time;

	// yes
	if (workstep.answer[0]?.after) {
		form.wordsYes = workstep.answer[0]?.do?.data; // 2
		form.wordsYesVideo = workstep.answer[0]?.do?.video;
		form.nextStepYes = workstep.answer[0]?.after?.id;
	} else {
		form.nextStepYes = workstep.answer[0]?.do?.id;
		form.wordsYesVideo = workstep.answer[0]?.do?.video;
	}

	// no
	if (workstep.answer[1]?.after) {
		form.wordsNo = workstep.answer[1]?.do?.data; // 4
		form.wordsNoVideo = workstep.answer[1]?.do?.video;
		form.nextStepNo = workstep.answer[1]?.after?.id;
	} else {
		form.nextStepNo = workstep.answer[1]?.do?.id;
		form.wordsNoVideo = workstep.answer[1]?.do?.video;
	}

	// other
	if (workstep.answer[2]?.after) {
		form.wordsOther = workstep.answer[2]?.do?.data; // 3
		form.wordsOtherVideo = workstep.answer[2]?.do?.video;
	} else if (tts) {
		form.wordsOtherVideo = workstep.answer[2]?.do?.video;
	}
	if (workstep.errorRemind?.after) {
		form.wordsEnd = workstep.errorRemind?.do?.data; // 5
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
		form.nextStepOther = workstep.errorRemind?.after?.id;
	} else {
		form.nextStepOther = workstep.errorRemind?.do?.id;
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
	}
	form.otherTimesMax = workstep.errorRemind?.maximum; // 4
	form.wordsOverTime = workstep.errorRemind?.timeout; // 4

	/** 变量存储处理开始 */
	form['aiSpeakVariableId'] = workstep.do.variableId;
	form['aiSpeakVariables'] = workstep.do.variables?.filter(item => form['aiSpeak']?.indexOf(`{{${item.name}}}`) !== -1);

	form['wordsYesVariableId'] = workstep.answer[0].do.variableId;
	form['wordsYesVariables'] = workstep.answer[0].do.variables?.filter(item => form['wordsYes']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsNoVariableId'] = workstep.answer[1].do.variableId;
	form['wordsNoVariables'] = workstep.answer[1].do.variables?.filter(item => form['wordsNo']?.indexOf(`{{${item.name}}}`) !== -1);

	form['wordsOtherVariableId'] = workstep.answer[2].do.variableId;
	form['wordsOtherVariables'] = workstep.answer[2].do.variables?.filter(item => form['wordsOther']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsEndVariableId'] = workstep.errorRemind.do.variableId;
	form['wordsEndVariables'] = workstep.errorRemind.do.variables?.filter(item => form['wordsEnd']?.indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */
	form.name = workstep.name;
};
InitModalMethod['collectShortVideo'] = initModalData;

/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}, tts = false) => {
	console.log(form, workstep);
	if (!form || !workstep || !workstep.do || !workstep.answer) {
		return;
	}

	workstep.do.data = form.aiSpeak; // 1

	workstep.timeout = form.waitTime; // 2

	workstep.after.data.data.data.time = form.collectShortVideoTime;

	// yes
	if ((form.wordsYes && !tts) || (form.wordsYesVideo && !tts) || (tts && form.wordsYes && form.wordsYesVideo)) {
		workstep.answer[0].do.data = form.wordsYes; // 2
		workstep.answer[0].do.video = form.wordsYesVideo;
		workstep.answer[0].after.id = form.nextStepYes;
	} else {
		workstep.answer[0].do = Object.assign({}, workstep.answer[0].after);
		workstep.answer[0].do.id = form.nextStepYes;
		workstep.answer[0].do.video = form.wordsYesVideo;
		delete workstep.answer[0].after;
	}

	// no
	if ((form.wordsNo && !tts) || (form.wordsNoVideo && !tts) || (tts && form.wordsNo && form.wordsNoVideo)) {
		workstep.answer[1].do.data = form.wordsNo; // 4
		workstep.answer[1].do.video = form.wordsNoVideo;
		workstep.answer[1].after.id = form.nextStepNo;
	} else {
		workstep.answer[1].do = Object.assign({}, workstep.answer[1].after);
		workstep.answer[1].do.id = form.nextStepNo;
		workstep.answer[1].do.video = form.wordsNoVideo;
		delete workstep.answer[1].after;
	}

	// other
	if ((form.wordsOther && !tts) || (form.wordsOtherVideo && !tts) || (tts && form.wordsOther && form.wordsOtherVideo)) {
		workstep.answer[2].do.data = form.wordsOther; // 3
		workstep.answer[2].do.video = form.wordsOtherVideo;
	} else {
		workstep.answer[2].do = Object.assign({}, workstep.answer[2].after);
		workstep.answer[2].do.video = form.wordsOtherVideo;
		delete workstep.answer[2].after;
	}
	if ((form.wordsEnd && !tts) || (form.wordsEndVideo && !tts) || (tts && form.wordsEnd && form.wordsEndVideo)) {
		workstep.errorRemind.after.id = form.nextStepOther;
		workstep.errorRemind.do.data = form.wordsEnd; // 5
		workstep.errorRemind.do.video = form.wordsEndVideo;
	} else {
		workstep.errorRemind.do = Object.assign({}, workstep.errorRemind.after);
		workstep.errorRemind.do.id = form.nextStepOther;
		workstep.errorRemind.do.video = form.wordsEndVideo;
		delete workstep.errorRemind.after;
	}
	workstep.errorRemind.maximum = form.otherTimesMax; // 4
	// timeout
	workstep.errorRemind.timeout = form.wordsOverTime; // 4

	// 预录制视频属性
	workstep.do.video = form.aiSpeakVideo;

	/** 变量存储处理开始 */
	workstep.do.variableId = form['aiSpeakVariableId'];
	workstep.do.variables = form['aiSpeakVariables']?.filter(item => form['aiSpeak'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[0].do.variableId = form['wordsYesVariableId'];
	workstep.answer[0].do.variables = form['wordsYesVariables']?.filter(item => form['wordsYes'].indexOf(`{{${item.name}}}`) !== -1);
	workstep.answer[1].do.variableId = form['wordsNoVariableId'];
	workstep.answer[1].do.variables = form['wordsNoVariables']?.filter(item => form['wordsNo'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[2].do.variableId = form['wordsOtherVariableId'];
	workstep.answer[2].do.variables = form['wordsOtherVariables']?.filter(item => form['wordsOther'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.errorRemind.do.variableId = form['wordsEndVariableId'];
	workstep.errorRemind.do.variables = form['wordsEndVariables']?.filter(item => form['wordsEnd'].indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */

	workstep.name = form.name;
};
ParseModalMethod['collectShortVideo'] = parseModalData;
// {
// 	"do": {
// 		"data": "短视频采集23",
// 		"command": "speak",
// 		"variables": [],
// 		"variableId": ""
// 	},
// 	"id": "collectShortVideo_23",
// 	"no": "collectShortVideo_23",
// 	"name": "",
// 	"type": "collectShortVideo",
// 	"after": {
// 		"data": {
// 			"data": {
// 				"cmd": "collect-short-video",
// 				"data": {
// 					"time": 30000
// 				}
// 			},
// 			"type": "peer-cmd"
// 		},
// 		"command": "send-message",
// 		"after": {
// 			"command": "receive-message"
// 		}
// 	},
// 	"answer": [{
// 		"do": {
// 			"data": "提交成功",
// 			"command": "speak",
// 			"variables": [],
// 			"variableId": ""
// 		},
// 		"key": ["ok"],
// 		"type": "yes",
// 		"after": {
// 			"id": "handWriting_27",
// 			"no": "handWriting_27",
// 			"type": "yes",
// 			"command": "jump-to"
// 		}
// 	}, {
// 		"do": {
// 			"data": "提交失败",
// 			"command": "speak",
// 			"variables": [],
// 			"variableId": ""
// 		},
// 		"key": ["reject"],
// 		"type": "no",
// 		"after": {
// 			"id": "handWriting_27",
// 			"no": "handWriting_27",
// 			"type": "no",
// 			"command": "jump-to"
// 		}
// 	}, {
// 		"do": {
// 			"data": "超时未提交",
// 			"command": "speak",
// 			"variables": [],
// 			"variableId": ""
// 		},
// 		"key": ["timeout"],
// 		"type": "other",
// 		"after": {
// 			"data": "",
// 			"command": "to-compare-answer"
// 		}
// 	}],
// 	"errorRemind": {
// 		"do": {
// 			"data": "等待超时超过最大次数",
// 			"command": "speak",
// 			"variables": [],
// 			"variableId": ""
// 		},
// 		"after": {
// 			"id": "handWriting_27",
// 			"no": "handWriting_27",
// 			"command": "jump-to"
// 		},
// 		"maximum": 1,
// 		"timeout": 3000
// 	},
// 	"timeout": 3000,
// 	"completed": true,
// 	"hasStartNode": false
// }
